import { genPrefixId } from '../../core/manager';
import type PaintBoard from '..';
import type { Paint } from '../utils';

// TODO all shape paint is by polygon|multipolygon
// 1 layer for all circle,rectangle,freedom : multipolygon
// 1 layer for each circle,rectangle,freedom : polygon
abstract class Shape {
  map: mapboxgl.Map | null;

  sourceId = '';
  layerId = '';

  paint: Paint | undefined; // 只记录关键点

  abstract DrawingStart(
    e: mapboxgl.MapMouseEvent,
    paintBoard: PaintBoard,
  ): Paint | undefined;

  abstract Drawing(
    e: mapboxgl.MapMouseEvent,
    paintBoard: PaintBoard,
  ): Paint | undefined;

  abstract DrawingEnd(
    e: mapboxgl.MapMouseEvent,
    paintBoard: PaintBoard,
  ): Paint | undefined;

  constructor(map: mapboxgl.Map) {
    this.map = map;
  }

  retPaint() {
    return this.paint;
  }

  initLayers() {
    if (!this.map) return;

    this.sourceId = genPrefixId('paint-source');
    this.layerId = genPrefixId('paint-layer');

    this.map.addSource(this.sourceId, {
      type: 'geojson',
      data: {
        type: 'FeatureCollection',
        features: [],
      },
    });
    this.map.addLayer({
      id: this.layerId,
      type: 'line',
      source: this.sourceId,
      paint: {
        'line-dasharray': [10, 5],
        'line-color': '#f00',
      },
    });
  }

  enter() {
    if (!this.map) return;
    this.initLayers();
  }

  exit() {
    if (!this.map) return;
    this.map.removeLayer(this.layerId);
    this.map.removeSource(this.sourceId);
  }
}

export default Shape;
